﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Web;
using System.IO;
using System.Linq;
using System.Drawing;
using System.Drawing.Imaging;

using RexToy.Configuration;
using RexToy.Resources;
using RexToy.Template.Text;

namespace RexToy.UI.Web
{
    public class ScriptHandler : IHttpHandler
    {
        private const string PATH = "RexToy.UI.Web.JavaScript";
        private const string RESOURCES = "Resources";
        private const string THEME = "theme";

        private const string IMG = "img";
        private const string CSS = "css";
        private const string LIB = "lib";
        //Extend:ajax request: private const string SVC = "svc";

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            var keys = context.Request.QueryString.AllKeys;
            if (keys.Contains(IMG))
            {
                ProcessImageRequest(context);
                return;
            }

            if (keys.Contains(CSS))
            {
                ProcessStyleRequest(context);
                return;
            }

            if (keys.Contains(LIB))
            {
                ProcessJsRequest(context);
                return;
            }
        }

        private static void ProcessJsRequest(HttpContext ctx)
        {
            //TODO:Cache script to improve perf/ const string
            string lib = ctx.Request[LIB];
            string js = Namespace.Combine(PATH, lib);
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(js))
            using (StreamReader reader = new StreamReader(stream))
            {
                string script = reader.ReadToEnd();
                ctx.Response.ContentType = "application/x-javascript";
                ctx.Response.Write(script);
            }
        }

        private void ProcessImageRequest(HttpContext ctx)
        {
            string theme = ctx.Request[THEME];
            if (string.IsNullOrEmpty(theme))
                theme = "default";
            string img = Namespace.Combine(PATH, RESOURCES, theme, ctx.Request[IMG]);

            string ext = Path.GetExtension(img).RemoveBegin('.');
            ctx.Response.ContentType = string.Format("image/{0}", ext);
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(img))
            using (Bitmap bmp = new Bitmap(stream))
            {
                bmp.Save(ctx.Response.OutputStream, TypeCast.ChangeType<ImageFormat>(ext));
            }
        }

        private void ProcessStyleRequest(HttpContext ctx)
        {
            string theme = ctx.Request[THEME];
            if (string.IsNullOrEmpty(theme))
                theme = "default";
            string css = Namespace.Combine(PATH, RESOURCES, theme, ctx.Request[CSS]);

            ctx.Response.ContentType = string.Format("text/css");
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(css))
            using (StreamReader reader = new StreamReader(stream))
            {
                string style = reader.ReadToEnd();
                StringTemplate t = style;
                t.Assign("scriptHandler", AppConfig.Config.ReadValue("web", "ajaxHandler"));


                ctx.Response.Write(t.Render());
            }
        }
    }
}
